//
//  CustomScrollViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/12/26.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "CustomScrollViewController.h"

@interface CustomScrollViewController ()
<UITableViewDelegate,UITableViewDataSource,
UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

proStrong UIView *containerView;
proStrong UIScrollView *outterScrollView;
proStrong UIView *headerView;
proStrong UITableView *listTableView;

proStrong UICollectionView *collectView;

proStrong UIPanGestureRecognizer *panGesture;

@end

@implementation CustomScrollViewController

- (void)addViews {
    
    self.title = @"UIScrollView内嵌一个UITableView";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, kScreenWidth-20, kScreenHeight-150)];
    [Utils resetView:self.containerView bgColor:[UIColor whiteColor] cornerRadius:0 borderWidth:1 borderColor:[UIColor blackColor]];
    [self.view addSubview:self.containerView];
    
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pagGestureAction:)];
    self.containerView.userInteractionEnabled = YES;
    [self.containerView addGestureRecognizer:self.panGesture];
    
    self.outterScrollView = [[UIScrollView alloc] initWithFrame:self.containerView.bounds];
    self.outterScrollView.scrollEnabled = NO;
    self.outterScrollView.bounces = YES;
    [self.containerView addSubview:_outterScrollView];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.outterScrollView.width, 250)];
    self.headerView.backgroundColor = [UIColor greenColor];
    [self.outterScrollView addSubview:self.headerView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectView = [[UICollectionView alloc] initWithFrame:self.headerView.bounds collectionViewLayout:layout];
    self.collectView.backgroundColor = [UIColor clearColor];
    [self.collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectCell"];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    [self.headerView addSubview:self.collectView];
    
    self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headerView.height, self.outterScrollView.width, self.outterScrollView.height-self.headerView.height)];
    self.listTableView.backgroundColor = [UIColor clearColor];
    self.listTableView.scrollEnabled = NO;
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.tableFooterView = [UIView new];
    [self.listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.outterScrollView addSubview:self.listTableView];
    
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 80)];
    tableHeaderView.backgroundColor = [UIColor yellowColor];
    UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:tableHeaderView.bounds];
    tableHeaderLabel.font = [UIFont boldSystemFontOfSize:20];
    tableHeaderLabel.text = @"TableViewHeader";
    tableHeaderLabel.textAlignment = NSTextAlignmentCenter;
    [tableHeaderView addSubview:tableHeaderLabel];
    self.listTableView.tableHeaderView = tableHeaderView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self addViews];
}

#pragma mark - 滑动手势

- (void)pagGestureAction:(UIPanGestureRecognizer *)panGesture {
    
    switch (panGesture.state) {
            
        case UIGestureRecognizerStateBegan:
        {
            break;
        }
            
        case UIGestureRecognizerStateChanged:
        {
            
            CGPoint trans = [panGesture translationInView:self.containerView];
            CGFloat outterContentOffsetY = self.outterScrollView.contentOffset.y;
            CGFloat tableContentOffsetY = self.listTableView.contentOffset.y;
            CGFloat headerHeight = self.headerView.height;
            CGFloat distance = fabs(trans.y);
            NSLog(@"===========================");
            NSLog(@"outterContentOffsetY:%.2f",outterContentOffsetY);
            NSLog(@"tableContentOffsetY:%.2f",tableContentOffsetY);
            NSLog(@"===========================");
            
            if (trans.y > 0) { //向下划动
                NSLog(@"___向下滑动：trans.y=%.2f",trans.y);
                
                
                CGFloat listOffset = 0;
                //列表还能滑动
                if (tableContentOffsetY > 0) {
                    if (distance < tableContentOffsetY) {
                        listOffset = distance;
                    } else {
                        listOffset = tableContentOffsetY;
                    }
                }
                self.listTableView.contentOffset = CGPointMake(0, tableContentOffsetY-listOffset);
                
                CGFloat oDistance = distance-listOffset;
                if (oDistance>0 && outterContentOffsetY>0) {
                    if (oDistance>outterContentOffsetY) {
                        oDistance = outterContentOffsetY;
                    }
                    self.outterScrollView.contentOffset = CGPointMake(0, outterContentOffsetY-oDistance);
                }
                
            } else if (trans.y < 0) { //向上滑动
                NSLog(@"___向上滑动：trans.y=%.2f",trans.y);
                
                
                CGFloat outterOffset = 0;
                if (outterContentOffsetY <= headerHeight) { //顶部还可以滚动
                    CGFloat maxOutterDis = headerHeight - outterContentOffsetY;//外部最大的可移动位移
                    if (distance<maxOutterDis) {
                        outterOffset = distance;
                    } else {
                        outterOffset = maxOutterDis;
                    }
                }
                
                //调整头部位置
                self.outterScrollView.contentOffset = CGPointMake(0, outterContentOffsetY+outterOffset);
                
                //调整列表位置大小
                self.listTableView.height = self.containerView.height - self.headerView.height + self.outterScrollView.contentOffset.y;
                
                CGFloat listOffset = distance - outterOffset;
                if (listOffset>0 &&
                    self.listTableView.contentSize.height>self.listTableView.frame.size.height &&
                    tableContentOffsetY<(self.listTableView.contentSize.height-self.listTableView.frame.size.height)) {
                    
                    CGFloat maxListOffset = self.listTableView.contentSize.height - self.listTableView.frame.size.height;
                    CGFloat offset = tableContentOffsetY + listOffset;
                    if (offset>maxListOffset) {
                        offset = maxListOffset;
                    }
                    self.listTableView.contentOffset = CGPointMake(0, offset);
                }
            }
            
            [panGesture setTranslation:CGPointZero inView:self.containerView];
            
            break;
        }
            
        default:
            break;
    }
}

#pragma mark - collectionView delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectCell" forIndexPath:indexPath];
    NSInteger r = arc4random()%254+1;
    NSInteger g = arc4random()%254+1;
    NSInteger b = arc4random()%254+1;
    cell.backgroundColor = RGB_INT(r, g, b);
    
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = (_collectView.height-60)/5;
    CGFloat width = (_collectView.width-30)/2;
    
    return CGSizeMake(width, height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

#pragma mark - tableView delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = [NSString stringWithFormat:@"section:%d,row:%d",(int)indexPath.section,(int)indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.listTableView.width, 30)];
    headerView.backgroundColor = [UIColor orangeColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, headerView.width-20, headerView.height)];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.text = [NSString stringWithFormat:@"section:%d",(int)section];
    [headerView addSubview:label];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}

@end
